﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;

namespace YJKFamilyComparison
{
	internal static class Utility
	{
		internal static string GetParameterValue(Parameter para)
		{
			string text = "";
			string result;
			try
			{
                switch ((int)para.StorageType)
				{
				case 0:
					result = text;
					break;
				case 1:
					result = para.AsInteger().ToString();
					break;
				case 2:
					result = para.AsValueString();
					break;
				case 3:
					result = para.AsString().ToString();
					break;
				case 4:
					result = para.AsElementId().ToString();
					break;
				default:
					result = text;
					break;
				}
			}
			catch (Exception)
			{
				result = text;
			}
			return result;
		}

		internal static KeyValuePair<int, int> GetCategoryIndex(ChildResultPackerDataStructure crpDataStructure, Category category)
		{
			for (int i = 0; i < crpDataStructure.ThisValue.Count; i++)
			{
				for (int j = 0; j < crpDataStructure.ThisValue.ElementAt(i).Value.Count; j++)
				{
					if (crpDataStructure.ThisValue.ElementAt(i).Value[j].Name == category.Name)
					{
						return new KeyValuePair<int, int>(i, j);
					}
				}
			}
			return new KeyValuePair<int, int>(-999, -999);
		}

		internal static KeyValuePair<int, int> GetFamilyNameIndex(ChildResultPackerDataStructure crpDataStructure, string familyname)
		{
			for (int i = 0; i < crpDataStructure.ThisValue.Count; i++)
			{
				for (int j = 0; j < crpDataStructure.ThisValue.ElementAt(i).Value.Count; j++)
				{
					if (crpDataStructure.ThisValue.ElementAt(i).Value[j].Name == familyname)
					{
						return new KeyValuePair<int, int>(i, j);
					}
				}
			}
			return new KeyValuePair<int, int>(-999, -999);
		}

		internal static void CompareAndFillNode_DFS(ref ResultPackerBase resultPackerOne, ResultPackerBase resultPackerTwo)
		{
			int count = resultPackerTwo.ChildPackers.ThisValue.Count;
			ResultPackerBase resultPackerBase = null;
			for (int i = 0; i < count; i++)
			{
				resultPackerBase = null;
				for (int j = 0; j < resultPackerTwo.ChildPackers.ThisValue.ElementAt(i).Value.Count; j++)
				{
					int key = resultPackerTwo.ChildPackers.ThisValue.ElementAt(i).Key;
					ResultPackerBase resultPackerBase2 = resultPackerTwo.ChildPackers.ThisValue[key][j];
					if (resultPackerBase2 == null)
					{
						return;
					}
					if (!resultPackerOne.ChildPackers.IfHave(resultPackerBase2))
					{
						resultPackerOne.ChildPackers.AddPacker(ResultPackerBase.CreatANoneValueNode(resultPackerBase2));
						resultPackerOne.ChildPackers.Order();
					}
					resultPackerBase = resultPackerOne.ChildPackers.GetResultPacker(resultPackerBase2);
					resultPackerBase.ChildPackers.Order();
					Utility.CompareAndFillNode_DFS(ref resultPackerBase, resultPackerBase2);
				}
			}
		}

		internal static ChildResultPackerDataStructure CreatANoneValueNodeChildStructure(ResultPackerBase input)
		{
			ChildResultPackerDataStructure childResultPackerDataStructure = new ChildResultPackerDataStructure();
			childResultPackerDataStructure.Clear();
			foreach (KeyValuePair<int, List<ResultPackerBase>> keyValuePair in input.ChildPackers.ThisValue)
			{
				foreach (ResultPackerBase input2 in keyValuePair.Value)
				{
					childResultPackerDataStructure.AddPacker(ResultPackerBase.CreatANoneValueNode(input2));
				}
			}
			return childResultPackerDataStructure;
		}
	}
}
